﻿<template>
    <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        draggable
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="仓库编码">
                        <el-input v-model="entityObj.warehouseCode" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="仓库名称" prop="warehouseName">
                        <el-input v-model="entityObj.warehouseName" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="所属机构" prop="orgName">
                        <el-input v-model="entityObj.orgName" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="仓库类型" label-width="130px" prop="warehouseType">
                        <el-select
                            placeholder="---请选择仓库类型---"
                            v-model="entityObj.warehouseType"
                            style="width: 100%"
                        >
                            <el-option label="---请选择---" value />
                            <el-option
                                v-for="item in optionsWarehouseType"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="是否外部托管仓" prop="isExternal">
                        <el-switch
                            v-model="entityObj.isExternal"
                            :active-value="true"
                            :inactive-value="false"
                        />
                        <sc-supplier-select
                            v-show="entityObj.isExternal"
                            placeholder="---请选择供应商---"
                            ref="orgselect"
                            :props="supplierProps"
                            :selectKeyValue="'supplierId'"
                            v-model="entityObj.supplier"
                            :multiple="false"
                            style="margin-left: 10px; width: 80%;"
                            @fatherMethod="supplierSelectProcess"
                        ></sc-supplier-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label-width="130px" label="仓库地址">
                        <el-input v-model="entityObj.address" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="仓库负责人">
                        <el-input v-model="entityObj.realName" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="联系方式">
                        <el-input v-model="entityObj.mobile" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="开启负库存" prop="isMinus">
                        <el-switch
                            v-model="entityObj.isMinus"
                            :active-value="true"
                            :inactive-value="false"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="130px" label="状态" prop="status">
                        <el-switch
                            v-model="entityObj.status"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="130px" label="备注">
                        <el-input v-model="entityObj.remarks" type="textarea" :rows="2" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                isMinus: false,
                isExternal: false,
                status: 1,
            },
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            supplierProps: {
                label: "supplierName",
                value: "supplierId",
            },
            //验证规则
            rules: {
                warehouseCode: [
                    { required: true, type: "string", message: "请填写" },
                ],
                warehouseName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                warehouseType: [
                    { required: true, type: "number", message: "请填写" },
                ],
                isExternal: [
                    { required: true, type: "boolean", message: "请填写" },
                ],
                supplierId: [
                    {
                        validator: (rule, value, callback) => {
                            // 如果 isExternal 为 true, 则 supplierId 为必填
                            if (this.isExternal && !value) {
                                callback(new Error("请填写供应商"));
                            } else {
                                callback();
                            }
                        },
                        message: "请填写供应商",
                    },
                ],
                status: [{ required: true, type: "number", message: "请填写" }],
                isMinus: [
                    { required: true, type: "boolean", message: "请填写" },
                ],
            },
            optionsWarehouseType: [],
        };
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                   
                    console.log(this.entityObj.isExternal)
                    console.log(this.entityObj.supplierId)
                    if(this.entityObj.isExternal&&(!this.entityObj.supplierId||this.entityObj.supplierId==0)){
                        
                        this.$message.error("请选择外部托管供应商");
                        return;
                    }
                    this.isSaveing = true;
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                this.mode == "Add"
                                    ? "warehouse/create"
                                    : "warehouse/update",
                                { ...this.entityObj }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.isSaveing = false;
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                }
            });
        },
        //供应商
        async supplierSelectProcess(rowData) {
            this.entityObj.supplier = { ...rowData };
            this.entityObj.supplierId = rowData.supplierId;
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["WarehouseType"] }
                );
                if (returnValue.status == 200) {
                    this.optionsWarehouseType = returnValue.response.find(
                        (p) => p.code == "WarehouseType"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //表单注入数据
        async setData(data) {
            this.entityObj = {
                ...data,
                supplier: {
                    supplierId: data.supplierId,
                    supplierName: data.supplierName,
                },
            };
            console.log(this.entityObj);
        },
    },
    async mounted() {
        this.entityObj.orgName = tool.data.get("USER_INFO").OrgName;
        this.initOptions();
    },
};
</script>
